<template>
  <div class="wrap_menu">
    <el-form ref="formData" :rules="rules" :model="formData" class="form-wrap" label-position="top" label-width="80px">
      <el-form-item :label="$t('configLazyLoad.process.nodeName')" prop="DisplayName">
        <el-input v-model.trim="formData.DisplayName"></el-input>
      </el-form-item>
      <el-form-item label="提示语音：">
        <el-select v-model="formData.Filename" placeholder="请选择" style="width: 100%;">
          <el-option
            label="请选择"
            value="">
          </el-option>
          <el-option
            v-for="item in ivr_sound.ivr_sound"
            v-if="item.SoundType == 'normal'"
            :label="item.DisplayName"
            :key="item._id"
            :value="item.Filename">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="按0前台转向：">
        <node-select-item :curPbx="curPbx" :ToPeerType="formData.ToPeerTypeWhenFrontOffice" :ToPeer="formData.ToPeerWhenFrontOffice" ref="nodeSelectZero"></node-select-item>
      </el-form-item>
      <el-form-item label="工号不存在转向：">
        <node-select-item :curPbx="curPbx" :ToPeerType="formData.ToPeerTypeWhenEmpnoNotExist" :ToPeer="formData.ToPeerWhenEmpnoNotExist" ref="nodeSelectExten"></node-select-item>
      </el-form-item>
      <el-form-item label="座席忙转向：">
        <node-select-item :curPbx="curPbx" :ToPeerType="formData.ToPeerTypeWhenEmpnoAgentBusy" :ToPeer="formData.ToPeerWhenEmpnoAgentBusy" ref="nodeSelectAgent"></node-select-item>
      </el-form-item>
      <el-form-item label="网关未注册也未绑定手机转向：">
        <node-select-item :curPbx="curPbx" :ToPeerType="formData.ToPeerTypeWhenEmpnoLogoff" :ToPeer="formData.ToPeerWhenEmpnoLogoff" ref="nodeSelectGateway"></node-select-item>
      </el-form-item>
    </el-form>
    <div class="footer">
      <el-button class='slide-box__button' @click='cancelClick'>
        取消
      </el-button>
      <el-button class='slide-box__button' type="primary" @click='saveClick'>
        保存
      </el-button>
    </div>
  </div>
</template>
<script>
  import nodeSelectItem from '../../base/nodeSelectItem.vue'
  import {deepClone} from 'src/utils/m7Utils.js'
  export default {
    name: 'voiceMailDeal',
    data () {
      return {
        formData: { // 采集工号并拨打节点
          _id: '',
          DisplayName: '',
          Exten: '',
          Filename: '',
          ToPeerTypeWhenFrontOffice: '', // 按0前台转向
          ToPeerWhenFrontOffice: '',
          ToPeerTypeWhenEmpnoNotExist: '', // 工号不存在转向
          ToPeerWhenEmpnoNotExist: '',
          ToPeerTypeWhenEmpnoAgentBusy: '', // 座席忙转向
          ToPeerWhenEmpnoAgentBusy: '',
          ToPeerTypeWhenEmpnoLogoff: '', // 网关未注册也未绑定手机转向
          ToPeerWhenEmpnoLogoff: ''
        },
        rules: {
          DisplayName: [
            { required: true, message: this.$t('configLazyLoad.process.processLabel7'), trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
      curPbx () {
        return this.$store.state.config.callCenter.currentPBX
      },
      Account () {
        return this.$store.state.session.user.account
      },
      ivr_sound () {
        return this.$store.state.session.ivrMap[this.curPbx].data
      }
    },
    components: {
      nodeSelectItem
    },
    props: {
      form: {
        type: Object,
        default: function () {
          return {
            _id: '',
            DisplayName: '',
            Exten: '',
            Filename: '', // 语音文件
            ToPeerTypeWhenFrontOffice: '', // 按0前台转向
            ToPeerWhenFrontOffice: '',
            ToPeerTypeWhenEmpnoNotExist: '', // 工号不存在转向
            ToPeerWhenEmpnoNotExist: '',
            ToPeerTypeWhenEmpnoAgentBusy: '', // 座席忙转向
            ToPeerWhenEmpnoAgentBusy: '',
            ToPeerTypeWhenEmpnoLogoff: '', // 网关未注册也未绑定手机转向
            ToPeerWhenEmpnoLogoff: ''
          }
        }
      }
    },
    methods: {
      initForm (obj) {
        this.formData = deepClone(obj)
        let flag = false
        if (this.ivr_sound.ivr_sound && this.ivr_sound.ivr_sound.length > 0) {
          this.ivr_sound.ivr_sound.forEach(item => {
            if (this.form.Filename === item.Filename) {
              flag = true
            }
          })
        }
        if (!flag) {
          this.formData.Filename = ''
        } else {
          this.formData.Filename = this.form.Filename
        }
      },
      cancelClick () {
        this.$emit('cancelClick')
      },
      // 更新pbx数据
      updateVuexPbx () {
        this.$store.dispatch('querySuperDialJobNum', {Account: this.Account, PBX: this.curPbx}).then(res => {
          if (res && res.success) {
            this.$store.commit('SET_IVRMAP_DETAIL', {PBX: this.curPbx, typeName: 'ivr_node_superDialJobNum', data: res.list || [], flag: 'data', node: this})
          }
          this.$emit('saveClick')
        })
      },
      saveClick () {
        this.$refs['formData'].validate((valid) => {
          if (valid) {
            let form = deepClone(this.formData)
            if (!form.id) {
              form.Account = this.Account
              form.PBX = this.curPbx
            }
            form.ToPeerTypeWhenFrontOffice = this.$refs.nodeSelectZero.ToPeerTypeThis
            form.ToPeerWhenFrontOffice = this.$refs.nodeSelectZero.ToPeerThis
            form.ToPeerTypeWhenEmpnoNotExist = this.$refs.nodeSelectExten.ToPeerTypeThis
            form.ToPeerWhenEmpnoNotExist = this.$refs.nodeSelectExten.ToPeerThis
            form.ToPeerTypeWhenEmpnoAgentBusy = this.$refs.nodeSelectAgent.ToPeerTypeThis
            form.ToPeerWhenEmpnoAgentBusy = this.$refs.nodeSelectAgent.ToPeerThis
            form.ToPeerTypeWhenEmpnoLogoff = this.$refs.nodeSelectGateway.ToPeerTypeThis
            form.ToPeerWhenEmpnoLogoff = this.$refs.nodeSelectGateway.ToPeerThis
            form.UpdateTime && delete form.UpdateTime
            this.$store.dispatch('updateSuperDialJobNum', form).then((res) => {
              if (res.success) {
                this.$message({
                  message: '保存成功',
                  type: 'success'
                })
                this.updateVuexPbx(res.data)
              } else {
                this.$message.error('保存失败')
              }
            })
          }
        })
      }
    },
    beforeMount () {
      this.initForm(this.form)
    },
    watch: {
      form (cur, old) {
        this.$refs['formData'].resetFields()
        this.initForm(cur)
        this.$refs.nodeSelectZero.init()
        this.$refs.nodeSelectExten.init()
        this.$refs.nodeSelectAgent.init()
        this.$refs.nodeSelectGateway.init()
      }
    }
  }
</script>
<style lang="stylus" scoped>
  @import "../../../../../../assets/common.styl"
  .note
    margin-top 20px
    padding 0 20px
    p
      color $c-prompt
    p.two
      margin-top 10px
  .wrap_menu
    width 100%
    padding-bottom 60px
    .form-wrap
      padding 20px
  .footer
    position absolute
    bottom 0
    left 0
    width 100%
    background rgba(255,255,255,1)
    box-shadow 0px -2px 4px 0px rgba(5,49,38,0.05)
    border-top 1px solid rgba(232,232,232,1)
    display flex
    justify-content center
    padding 13px 0
    box-sizing border-box
    .slide-box__button
      padding 8px 20px 8px
      box-sizing border-box
</style>
